﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcBlog.Models;
using PagedList;

namespace MvcBlog.Controllers
{ 
    public class PostsController : Controller
    {
        private BlogDbContext db = new BlogDbContext();
        private string errMsg = @"Unable to save changes. Please try again later.
                                If the problem persists see your system administrator.";

        //
        // GET: /Posts/

        public ViewResult Index(int? page)
        {
            var posts = db.Posts.OrderByDescending(p => p.PostID).Include(p => p.Category);
            int pageSize = 3;
            int pageNumber = (page ?? 1);

            return View(posts.ToPagedList(pageNumber,pageSize)); //View(posts.ToList());
        }

        //
        // GET: /Posts/Details/5

        public ViewResult Details(int id)
        {
            Post post = db.Posts.Find(id);
            return View(post);
        }

        //
        // GET: /Posts/Create

        public ActionResult Create()
        {
            ViewBag.CategoryID = new SelectList(db.PostCategories, "ID", "Name");
            return View();
        } 

        //
        // POST: /Posts/Create

        [HttpPost]
        public ActionResult Create(Post post)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.Posts.Add(post);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", errMsg);
            }

            ViewBag.CategoryID = new SelectList(db.PostCategories, "ID", "Name", post.CategoryID);
            return View(post);
        }
        
        //
        // GET: /Posts/Edit/5
 
        public ActionResult Edit(int id)
        {
            Post post = db.Posts.Find(id);
            ViewBag.CategoryID = new SelectList(db.PostCategories, "ID", "Name", post.CategoryID);
            return View(post);
        }

        //
        // POST: /Posts/Edit/5

        [HttpPost]
        public ActionResult Edit(Post post)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.Entry(post).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", errMsg);
            }

            ViewBag.CategoryID = new SelectList(db.PostCategories, "ID", "Name", post.CategoryID);
            return View(post);
        }

        //
        // GET: /Posts/Delete/5
 
        public ActionResult Delete(int id)
        {
            Post post = db.Posts.Find(id);
            return View(post);
        }

        //
        // POST: /Posts/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Post post = db.Posts.Find(id);
            db.Posts.Remove(post);
            db.SaveChanges();
            return RedirectToAction("Index");
        }


        public ActionResult Search(string SearchString)
        {
            var posts = db.Posts.Where(p => p.Title.Contains(SearchString)
                                        || p.Content.Contains(SearchString));
            return View(posts.ToArray());
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}